//
//  NCPageControl.m
//  
//
//  Created by tianpeng zhang on 11-12.14.
//  Copyright 2011 Etop. All rights reserved.
//

#import "NCPageControl.h"

@interface NCPageControl(private)

- (void) updateDots;

@end


@implementation NCPageControl

@synthesize imagePageStateNormal,imagePageStateHightlighted;
@synthesize dotSize;
@synthesize backImage;
@synthesize dotSpace;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code. 
        dotSize = CGSizeZero;
        dotSpace = 5;
    }
    return self;
}

- (void) setImagePageStateNormal:(UIImage *)image
{
	[imagePageStateNormal release];
	imagePageStateNormal = [image retain];
	[self updateDots];
}

- (void) setImagePageStateHightlighted:(UIImage *)image
{
	[imagePageStateHightlighted release];
	imagePageStateHightlighted = [image retain];
	[self updateDots];
}

- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
	[super endTrackingWithTouch:touch withEvent:event];
	[self updateDots];
}

- (void) updateDots
{
	if (imagePageStateNormal || imagePageStateHightlighted) {
		NSArray *subView = self.subviews;
		
		for (int i = 0; i < [subView count]; i++) {
			UIImageView *dot = [subView objectAtIndex:i];
            if (![dot isKindOfClass:[UIImageView class]]) {
                continue;
            }
            [dot setImage:(self.currentPage == i ? imagePageStateHightlighted : imagePageStateNormal)];
//            dot.image = (self.currentPage == i ? imagePageStateHightlighted : imagePageStateNormal); 	
            if (dotSize.width == 0) {
                dot.frame = CGRectMake(16*i+dotSpace,9,16,16);; //zt
            }else{
                dot.frame = CGRectMake((dotSize.width+dotSpace)*i, 9, dotSize.width, dotSize.height);
            }
           // 100, 426, 120, 34
		}
        
	}
}

-(void)drawRect:(CGRect)rect{
	if (imagePageStateNormal || imagePageStateHightlighted) {
		NSArray *subView = self.subviews;
		
		for (int i = 0; i < [subView count]; i++) {
			UIImageView *dot = [subView objectAtIndex:i];
            if (![dot isKindOfClass:[UIImageView class]]) {
                continue;
            }
            dot.image = (self.currentPage == i ? imagePageStateHightlighted : imagePageStateNormal); 	
            if (dotSize.width == 0) {
                dot.frame = CGRectMake(16*i+dotSpace,9,16,16);; //zt
            }else{
                dot.frame = CGRectMake((dotSize.width+dotSpace)*i, 9, dotSize.width, dotSize.height);
            }
		}
        
	}
}
- (void)dealloc {
	[imagePageStateNormal release];
	imagePageStateNormal = nil;
	[imagePageStateHightlighted release];
	imagePageStateHightlighted = nil;
    [super dealloc];
}


@end
